package com.insyte.config;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.facebook.FacebookSdk;
import com.google.gson.Gson;
import com.insyte.model.User;

/**
 * Created by Administrator on 22/06/2015.
 */
public class InsyteApp extends Application {

    public static User userGlobal;
    private static InsyteApp mInstance;
    private SharedPreferences mSharedPreferences;
    private static final String USERINFO = "user_info";
    private SharedPreferences.Editor editor;
    private Gson gson;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        mSharedPreferences = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
        editor = mSharedPreferences.edit();
        gson = new Gson();
        FacebookSdk.sdkInitialize(this);
    }

    public static InsyteApp getInstance() {
        if (mInstance == null) {
            mInstance = new InsyteApp();
        }
        return mInstance;
    }

    public void saveUser(User user) {
        userGlobal = user;
        String jsUser = gson.toJson(user);
        editor.putString(USERINFO, jsUser);
        editor.commit();
    }

    /* public User getUser() {
        if (userGlobal != null) return userGlobal;
        else {
//            User.getUserInformation(mSharedPreferences);
//            if (userGlobal == null) {
            String js = mSharedPreferences.getString(USERINFO, "");
            userGlobal = gson.fromJson(js, User.class);
            if (userGlobal == null) {
                userGlobal = new User();
            }
            //}
        }
        return userGlobal;
    }
 */
 public User getUser() {
        if (userGlobal != null) return userGlobal;
        else {
//            User.getUserInformation(mSharedPreferences);
//            if (userGlobal == null) {
            String js = mSharedPreferences.getString(USERINFO, "");
            userGlobal = gson.fromJson(js, User.class);
            if (userGlobal == null) {
                userGlobal = new User();
            }
            //}
        }
        return userGlobal;
    }
    //save fb Token

    public void saveFacebookToken(SharedPreferences preference, String token) {
        SharedPreferences.Editor prefsEditor = preference.edit();
        prefsEditor.putString(Constants.KEY_FB_TOKEN, token);
        prefsEditor.commit();
    }

    public static String getFacebookToken(SharedPreferences preference) {
        try {
            String token = preference.getString(Constants.KEY_FB_TOKEN, "");
            return token;
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }

    // Setter - Getter Type Login, SignUp
    public static InsyteEnums.TypeLogin toTypeLoginEnum(String myEnumString) {
        try {
            return InsyteEnums.TypeLogin.valueOf(myEnumString);
        } catch (Exception ex) {
            return InsyteEnums.TypeLogin.NONE;
        }
    }

    public static void setTypeLoginEnum(Context context, InsyteEnums.TypeLogin myEnum) {
        SharedPreferences sp = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Constants.KEY_TYPE_LOGIN, myEnum.toString());
        editor.commit();
    }

    public static InsyteEnums.TypeLogin getTypeLoginEnum(Context context) {
        SharedPreferences sp = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        String myEnumString = sp.getString(Constants.KEY_TYPE_LOGIN, InsyteEnums.TypeLogin.NONE.toString());
        return toTypeLoginEnum(myEnumString);
    }

}
